package com.jhhc.StormSurgeForecast.AstronomicalTide.Util;


import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.HighAndLowTides;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.HighAndLowTidesData;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.HighAndLowTidesDatas;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//获取高低潮方法
@Service
public class GetHighAndLowTides {
    //获取高低潮方法
    public List<HighAndLowTides> getHIghAndLowTides(List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList){
        List<HighAndLowTides> highAndLowTidesList = new ArrayList<>();
        //根据日期分组
        Map<String, List<BDMS_ST_ASTROTD_F>> DayListGroup = bdmsStAstrotdFList.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay, Collectors.toList()));
        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> List : DayListGroup.entrySet()) {
            HighAndLowTides highAndLowTides = new HighAndLowTides();
            HighAndLowTidesData  highAndLowTidesData = new HighAndLowTidesData();
            highAndLowTides.setTime(List.getKey());
            List.getValue().sort(Comparator.comparing(BDMS_ST_ASTROTD_F::getYmdh));
             for (int i=0;i<List.getValue().size();i++){
               //  System.out.println(List.getValue().get(i));
                     switch (List.getValue().get(i).getHltdmk()){
                         case 1:
                             HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                             data.setTm(List.getValue().get(i).getYmdh());
                             data.setValue(List.getValue().get(i).getFtdz());
                             data.setHltdmk(List.getValue().get(i).getHltdmk());
                             highAndLowTidesData.setHigherHighTide(data);
                             highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                             break;
                         case 2:
                             HighAndLowTidesDatas data2 = new HighAndLowTidesDatas();
                             data2.setTm(List.getValue().get(i).getYmdh());
                             data2.setValue(List.getValue().get(i).getFtdz());
                             data2.setHltdmk(List.getValue().get(i).getHltdmk());
                             highAndLowTidesData.setLowHighTide(data2);
                             highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                             break;
                         case 3:
                             HighAndLowTidesDatas data3 = new HighAndLowTidesDatas();
                             data3.setTm(List.getValue().get(i).getYmdh());
                             data3.setValue(List.getValue().get(i).getFtdz());
                             data3.setHltdmk(List.getValue().get(i).getHltdmk());
                             highAndLowTidesData.setHighAndLowTide(data3);
                             highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                             break;
                         case 4:
                             //HighAndLowTidesData  highAndLowTidesData = new HighAndLowTidesData();
                             HighAndLowTidesDatas data4 = new HighAndLowTidesDatas();
                             data4.setTm(List.getValue().get(i).getYmdh());
                             data4.setValue(List.getValue().get(i).getFtdz());
                             data4.setHltdmk(List.getValue().get(i).getHltdmk());;
                             highAndLowTidesData.setLowerLowTide(data4);
                             highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                            // System.out.println(highAndLowTides.getHighAndLowTidesData());
                             break;
                         default:
                             break;
                     }
                 }

            highAndLowTidesList.add(highAndLowTides);
        }
          //  highAndLowTidesList.add(highAndLowTides);
        highAndLowTidesList.sort(Comparator.comparing(HighAndLowTides::getTime));
        return  highAndLowTidesList;
    }
    //获取实测高低潮
    //获取高低潮方法
    public List<HighAndLowTides> getMeasuredHIghAndLowTides(List<ST_TIDE_R> st_tide_rList){
        List<HighAndLowTides> highAndLowTidesList = new ArrayList<>();
        Map<String, List<ST_TIDE_R>> DayListGroup = st_tide_rList.stream().collect(Collectors.groupingBy(ST_TIDE_R::getDay, Collectors.toList()));
        for (Map.Entry<String, List<ST_TIDE_R>> List : DayListGroup.entrySet()) {
            HighAndLowTides highAndLowTides = new HighAndLowTides();
            HighAndLowTidesData  highAndLowTidesData = new HighAndLowTidesData();
            highAndLowTides.setTime(List.getKey());
            for (int i=0;i<List.getValue().size();i++){
                if (List.getValue().get(i).getHltdmk()!=null){
                    switch (List.getValue().get(i).getHltdmk()){
                        case 1:
                            HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                            data.setTm(List.getValue().get(i).getTm());
                            data.setValue(List.getValue().get(i).getTdz());
                            data.setHltdmk(List.getValue().get(i).getHltdmk());
                            highAndLowTidesData.setHigherHighTide(data);
                            highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                            break;
                        case 2:
                            // HighAndLowTidesData  highAndLowTidesData2 = new HighAndLowTidesData();
                            HighAndLowTidesDatas data2 = new HighAndLowTidesDatas();
                            data2.setTm(List.getValue().get(i).getTm());
                            data2.setValue(List.getValue().get(i).getTdz());
                            data2.setHltdmk(List.getValue().get(i).getHltdmk());
//                             highAndLowTidesData2.setLowHighTides(data2);
//                             highAndLowTides.setHighAndLowTidesData(highAndLowTidesData2);
                            highAndLowTidesData.setLowHighTide(data2);
                            highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                            break;
                        case 3:
                            // HighAndLowTidesData  highAndLowTidesData3 = new HighAndLowTidesData();
                            HighAndLowTidesDatas data3 = new HighAndLowTidesDatas();
                            data3.setTm(List.getValue().get(i).getTm());
                            data3.setValue(List.getValue().get(i).getTdz());
                            data3.setHltdmk(List.getValue().get(i).getHltdmk());
                            highAndLowTidesData.setHighAndLowTide(data3);
                            highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                            break;
                        case 4:
                            //HighAndLowTidesData  highAndLowTidesData = new HighAndLowTidesData();
                            HighAndLowTidesDatas data4 = new HighAndLowTidesDatas();
                            data4.setTm(List.getValue().get(i).getTm());
                            data4.setValue(List.getValue().get(i).getTdz());
                            data4.setHltdmk(List.getValue().get(i).getHltdmk());
                            highAndLowTidesData.setLowerLowTide(data4);
                            highAndLowTides.setHighAndLowTidesData(highAndLowTidesData);
                            break;
                    }
                }
            }
            highAndLowTidesList.add(highAndLowTides);
        }
        //  highAndLowTidesList.add(highAndLowTides);
        highAndLowTidesList.sort(Comparator.comparing(HighAndLowTides::getTime));
        return  highAndLowTidesList;



    }




}
